package howl.adaptor;

import howl.types.HowlUser;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import compiler.nonterminals.CreateStmt;
import compiler.nonterminals.DeleteStmt;
import compiler.nonterminals.InsertStmt;


public class EmailAdaptor extends DuplexAdaptor {

	public EmailAdaptor(HowlUser u) {
		super(u);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<Command> get() {
		return null;
	}

	@Override
	public boolean put(String j) {
		if(j instanceof String) {
			String str = String.class.cast(j);
		
        try {
        	Properties props = new Properties(); 
            Session session = Session.getDefaultInstance(props, null); 
    		Message msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("UoWhowl@googlemail.com", "Howl Admin"));
			 msg.addRecipient(Message.RecipientType.TO,
	                 new InternetAddress(this.user.getEmailAddress(), this.user.getUsers().getNickname()));
	        msg.setSubject("Message from Howl Administrator");
	        msg.setText(str);
	        Transport.send(msg);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
		return true;
		}
		return false;
	}

	@Override
	public void doCreate(CreateStmt ht) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doDelete(DeleteStmt ht) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doInsert(InsertStmt ht) {
		// TODO Auto-generated method stub
		
	}

}
